package com.kfm.hotel.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kfm.hotel.domain.SysMenu;
import com.kfm.hotel.domain.SysRoleMenu;
import com.kfm.hotel.mapper.SysMenuMapper;
import com.kfm.hotel.service.SysMenuService;
import com.kfm.hotel.service.SysRoleMenuService;
import io.swagger.models.auth.In;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 10854
* @description 针对表【sys_menu(菜单表)】的数据库操作Service实现
* @createDate 2023-06-16 10:59:07
*/
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
    implements SysMenuService{

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Override
    public List<SysMenu> getAllMenusByTree() {
        // 查询所有的一级菜单
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        wrapper.orderByAsc("order_num");
        List<SysMenu> menus = list(wrapper);

        for (SysMenu menu : menus) {
            // 查询系统管理下的子菜单
            QueryWrapper<SysMenu> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("parent_id",menu.getId());
            wrapper1.orderByAsc("order_num");
            List<SysMenu> list = list(wrapper1);
            menu.setChildren(list);
        }

        return menus;
    }

    @Override
    public List<SysMenu> getAllMenusByTree2() {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("parent_id","order_num");
        // 所有的菜单
        List<SysMenu> menus = list(wrapper);
        List<SysMenu> result = new ArrayList<>();
        // 构建菜单树
        for (SysMenu menu : menus) {
            // 如果是一级菜单
            if (menu.getParentId() == 0) {
                // 查询子菜单
                List<SysMenu> children = getChildren(menu, menus);
                menu.setChildren(children);
                result.add(menu);
            }
        }
        return result;
    }
    @Override
    public List<SysMenu> getAllMenusByUserId(Integer userId){
        return getBaseMapper().getAllMenusByUserId(userId);
    }

    @Override
    public List<Tree<Integer>> getTreeMenuWithHutool(Integer userId) {
        // 查询用户对应角色的所有菜单
        List<SysMenu> list = getAllMenusByUserId(userId);
//        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
//        wrapper.orderByAsc("parent_id","order_num");
//        List<SysMenu> list = list(wrapper);

        TreeNodeConfig treeNodeConfig = getTreeNodeConfig();

        List<Tree<Integer>> build = TreeUtil.build(list, 0, treeNodeConfig, (menu, tree) -> {
            tree.setId(menu.getId())
                    .setName(menu.getName())
                    .setParentId(menu.getParentId())
                    .setWeight(menu.getOrderNum());
            tree.putExtra("url", menu.getUrl());
            tree.putExtra("icon", menu.getIcon());
            tree.putExtra("type", menu.getType());
            tree.putExtra("perms", menu.getPerms());
            tree.putExtra("target", menu.getTarget());

        });
        return build;
    }

    @NotNull
    private static TreeNodeConfig getTreeNodeConfig() {
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        treeNodeConfig.setWeightKey("orderNum");
        treeNodeConfig.setIdKey("id");
        return treeNodeConfig;
    }

    @Override
    public List<Tree<Integer>> grantTreeMenu() {
        // 查询所有菜单
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("parent_id","order_num");
        // 所有的菜单
        List<SysMenu> list = list(wrapper);

        //配置
        TreeNodeConfig treeNodeConfig = getTreeNodeConfig();

        List<Tree<Integer>> build = TreeUtil.build(list, 0, treeNodeConfig, (menu, tree) -> {
            tree.setId(menu.getId())
                    .setName(menu.getName())
                    .setParentId(menu.getParentId())
                    .setWeight(menu.getOrderNum());
            tree.putExtra("title", menu.getName());
            // 是否选中？
//            if (menuIds.contains(menu.getId()) && 0 != menu.getParentId()){
//                tree.putExtra("checked", true);
//            }
        });
        return build;
    }

    @Override
    public List<Object> extracted(Integer roleId) {
        // 查询角色拥有的菜单
        QueryWrapper<SysRoleMenu> wrapper1 = new QueryWrapper<>();
        wrapper1.select("menu_id");
        wrapper1.eq("role_id", roleId);
        List<Object> menuIds = sysRoleMenuService.listObjs(wrapper1);

        return menuIds;
    }

    private List<SysMenu> getChildren(SysMenu menu, List<SysMenu> menus) {
        // 子菜单
        List<SysMenu> children = new ArrayList<>();
        for (SysMenu sysMenu : menus) {
            if (sysMenu.getParentId().equals(menu.getId())) {
                // 我们目前只有二级菜单
//                List<SysMenu> c = getChildren(sysMenu, menus);
//                menu.setChildren(c);
                // 添加到子菜单
                children.add(sysMenu);
            }
        }
        return children;
    }
}




